/*
 * Copyright (c) 2017 Qunar.com. All Rights Reserved.
 */
package com.guoyu.project.web.admin;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.guoyu.project.model.School;
import com.guoyu.project.model.enums.ProjectType;
import com.guoyu.project.service.SchoolService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Maps;
import com.guoyu.project.common.pagination.Pagination;
import com.guoyu.project.common.utils.JsonUtil;
import com.guoyu.project.common.web.APIResponse;
import com.guoyu.project.model.Article;
import com.guoyu.project.model.enums.ArticleMinorType;
import com.guoyu.project.model.enums.ArticleType;
import com.guoyu.project.service.ArticleService;
import com.guoyu.project.service.FileUploadService;

/**
 * @author: libo Date: 17-5-26 Time: 下午3:45
 * @version: \$Id$
 */
@Controller
@RequestMapping("admin/article")
public class ArticleController extends BaseController {

    private final static Logger logger = LoggerFactory.getLogger(NewsController.class);

    @Resource
    private ArticleService articleService;
    @Resource
    private SchoolService schoolService;

    @Resource
    private FileUploadService fileUploadService;

    @RequestMapping("list")
    @ResponseBody
    public Pagination<Article> list(@RequestParam(defaultValue = "0") int offset,
            @RequestParam(defaultValue = "10") int limit, ArticleType majorType, String title,
            @RequestParam(defaultValue = "-1", required = false) int type) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("majorType", majorType);
        params.put("title", StringUtils.trim(title));
        for (ArticleMinorType minorType : majorType.getMinorTypes()) {
            if (minorType.getCode() == type) {
                params.put("minorType",minorType);
            }
        }
        return articleService.queryArticles(offset, limit, params);
    }

    /**
     * 跳到添加今日资讯填写页
     *
     * @return
     */
    @RequestMapping("add")
    public String add(@RequestParam(defaultValue = "0", required = false) int schoolId,
            @RequestParam(defaultValue = "0", required = false) int projectId,
            @RequestParam(defaultValue = "0", required = false) int minorTypeCode, ArticleType majorType, Model model) {
        model.addAttribute("majorType", majorType);
        model.addAttribute("typeList", majorType.getMinorTypes());

        if (majorType == ArticleType.PROJECT) {
            School school = schoolService.queryById(schoolId);
            model.addAttribute("school", school);
            ProjectType minorType = ProjectType.codeOf(minorTypeCode);
            model.addAttribute("minorType", minorType);
            if (minorType != ProjectType.NEWS) {
                return "admin/article/add_project";
            }
            Article article = articleService.queryById(projectId);
            model.addAttribute("project", article);
        }

        return "admin/article/add";
    }

    /**
     * 保存今日资讯信息
     *
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public APIResponse save(@RequestParam(value = "file", required = false) MultipartFile picture, Article article) {
        logger.info("提交保存今日资讯信息：{}", JsonUtil.encode(article));
        if (StringUtils.isBlank(article.getTitle())) {
            return APIResponse.returnFail("新闻标题不能为空");
        }
        String imgPath = fileUploadService.upload(picture);
        article.setPicture(imgPath);
        articleService.saveArticle(article);
        return APIResponse.returnSuccess();
    }

    /**
     * 跳到修改今日资讯信息页面
     *
     * @return
     */
    @RequestMapping("edit")
    public String edit(@RequestParam(defaultValue = "0", required = false) int minorTypeCode, int articleId,
            Model model) {
        Article article = articleService.queryById(articleId);
        model.addAttribute("article", article);
        model.addAttribute("typeList", article.getMajorType().getMinorTypes());
        if (article.getMajorType() == ArticleType.PROJECT) {
            ProjectType minorType = ProjectType.codeOf(minorTypeCode);
            model.addAttribute("minorType", minorType);
            if (minorType != ProjectType.NEWS) {
                return "admin/article/edit_project";
            }
            model.addAttribute("projectList", articleService.queryAllProject());
        }

        if (article.getMajorType() == ArticleType.PROGRESS) {
            return "admin/article/edit_progress";
        }
        return "admin/article/edit";
    }

    /**
     * 更新今日资讯信息
     *
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public APIResponse update(@RequestParam(value = "file", required = false) MultipartFile picture, Article article) {
        logger.info("提交修改今日资讯信息：{}", JsonUtil.encode(article));
        String imgPath = StringUtils.defaultIfBlank(fileUploadService.upload(picture), article.getPicture());
        article.setPicture(imgPath);
        articleService.updateArticle(article);
        return APIResponse.returnSuccess();
    }

    /**
     * 删除今日资讯信息
     *
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public APIResponse delete(int articleId) {
        articleService.deleteById(articleId);
        return APIResponse.returnSuccess();
    }
}
